package com.xyzh05.webapp.action;

import java.util.List;

import net.sf.json.JSONArray;

import com.xyzh05.model.Major;
import com.xyzh05.model.School;
import com.xyzh05.model.User;
import com.xyzh05.service.MajorManager;
import com.xyzh05.service.SchoolManager;
import com.xyzh05.service.UserManager;
import com.xyzh05.util.JSONUtil;

public class AutoCompleteAction extends BaseAction {
	private static final long serialVersionUID = -6784389952061800188L;
	private UserManager userManager;
	private SchoolManager schoolManager ; 
	private MajorManager majorManager ; 
	private String keyword;
	private String level; 
	private String jsonStr;

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	
	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getJsonStr() {
		return jsonStr;
	}

	public void setJsonStr(String jsonStr) {
		this.jsonStr = jsonStr;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public UserManager getUserManager() {
		return userManager;
	}
	
	
	public MajorManager getMajorManager() {
		return majorManager;
	}

	public void setMajorManager(MajorManager majorManager) {
		this.majorManager = majorManager;
	}

	public SchoolManager getSchoolManager() {
		return schoolManager;
	}

	public void setSchoolManager(SchoolManager schoolManager) {
		this.schoolManager = schoolManager;
	}

	public String autoCompletion(){
		List<User> userList = userManager.queryUserToAutoComplete(keyword, level);
		try {
			jsonStr = JSONUtil.getJsonStr(userList);			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String querySchoolName(){
		try {
			List<School> schoolList = schoolManager.querySchoolAutoComplete(keyword);
			jsonStr = JSONArray.fromObject(schoolList).toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String queryMajorName(){
		try {
			List<Major> majorList = majorManager.queryMajorAutoComplete(keyword);
			jsonStr = JSONArray.fromObject(majorList).toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
}
